<?php
	include_once '../config.inc.php';
	print_r($_FILES);
	
	//上传文件操作.注意表单操作.
	if(!empty($_FILES) && $_FILES['upfile']['error'] == 0){
		//echo $_FILES['upfile']['tmp_name'].'<br>';
		//echo ROOT.'upload/'.$_FILES['upfile']['name'];
		//有先删除
		if(is_file(iconv('utf-8', 'gb2312', $_FILES['upfile']['name']))){
			unlink(iconv('utf-8', 'gb2312', $_FILES['upfile']['name']));
		}
		//复制文件
		
		//生成日期时间+随机位数文件名
		function upfilename($len){
			$tempstr = date('YmdHis',time()).'_';
			$chars = '0123456789abcdefghijklmnopqrstuvwxyz';
			$max = strlen($chars) - 1;
			PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    		for($i = 0; $i < $len; $i++) {
        		$tempstr .= $chars[mt_rand(0, $max)];
    		}
			return $tempstr;
		}
		$filename =  ROOT.'upload/'.upfilename(5).'.'.substr($_FILES['upfile']['name'],strripos($_FILES['upfile']['name'], '.')+1);
		//copy($_FILES['upfile']['tmp_name'], ROOT.'upload/'.iconv('utf-8', 'gb2312', $_FILES['upfile']['name']));
		copy($_FILES['upfile']['tmp_name'], $filename);
		
		//调整大小
		//$filename = ROOT.'upload/'.$_FILES['upfile']['name'];
		//获得图片信息
		$imginfo = getimagesize($filename);//怎么试中文都不行
		//print_r($imginfo);//Array ( [0] => 826 [1] => 510 [2] => 3 [3] => width="826" height="510" [bits] => 8 [mime] => image/png )
		
		$img = imagecreatetruecolor(100, 100);//创建新大小的图片
		$srcimg = imagecreatefromjpeg($filename);//取得原图片
		imagecopyresampled($img, $srcimg, 0, 0, 0, 0, 100, 100, $imginfo[0], $imginfo[1]);//重新COPY限定大小
		
		$pos = strripos($filename,'.'); 		//获取到文件名的位置,最后一个点出现的位置
		$temp = substr($filename, $pos+1); 		//获取后缀格式
  		$filename = substr($filename,0,$pos); 	//获取文件名不带后缀
		
		imagejpeg($img,$filename.'-min.'.$temp);//输出小图到文件夹中
	}


?>

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>Document</title>
</head>
<body>
	<form action="" method="post" enctype="multipart/form-data">
		<input type="hidden" name="MAX-FILE_SIZE" value="2097152" />
		上传文件:<input type="file" name="upfile" />
		<input type="submit" name="subfile" value="上传"/>
	</form>
</body>
</html>